package cn.jiahua.test14;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class Test4 {
    /**
     * 同步队列
     * @param args
     */
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue =new SynchronousQueue(true);// 同步队列

        //放元素
        new  Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"put a");
                blockingQueue.put("a");
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName()+"put b");
                blockingQueue.put("b");
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName()+"put c");
                blockingQueue.put("c");
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"生产者").start();
        //取数据
        new  Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"获取数据"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"获取数据"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"获取数据"+blockingQueue.take());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"消费者").start();





    }


}
